package com.yxkj.syh.app.huarong.activities.links;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.syh.app.basic.BR;
import com.syh.app.basic.base.BaseFragment;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.LinkListAdp;
import com.yxkj.syh.app.huarong.bean.Linkman;
import com.yxkj.syh.app.huarong.bean.PagesRequest;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.databinding.FragmentLinksBinding;

import java.util.ArrayList;

/**
 * 联系人
 */
public class LinksFragment extends BaseFragment<FragmentLinksBinding,LinksVM> {

    PagesRequest linksRequest = new PagesRequest();
    private LinkListAdp mLinkListAdp;

    public LinksFragment() {
        // Required empty public constructor
    }

    public static LinksFragment newInstance() {
        LinksFragment fragment = new LinksFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.linksVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_links;
    }

    @Override
    protected void initView() {
        mVDBinding.statusBarHolder.getLayoutParams().height = ImmersionBar.getStatusBarHeight(getActivity());

        mLinkListAdp = new LinkListAdp();
        mVDBinding.rvLinks.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mLinkListAdp.bindToRecycleView(mVDBinding.rvLinks);
        mLinkListAdp.setOnViewClick(new LinkListAdp.OnViewClick() {
            @Override
            public void onItem(Linkman linkman) {
                ARouter.getInstance().build(ArouterPath.LINK_DETAIL_ACTIVITY).withLong("id",linkman.getId()).withBoolean("isFirst",false).navigation();
            }

            @Override
            public void onCall(String mobile) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mobile));
                startActivity(intent);
            }
        });

        mVDBinding.srlLinks.setOnLoadMoreListener(refreshLayout -> {
            linksRequest.setPageIndex(linksRequest.getPageIndex().intValue() + 1);
            mViewModel.linkList(linksRequest);
        });
        mVDBinding.srlLinks.setOnRefreshListener(refreshLayout -> {
            linksRequest.setPageIndex(1);
            mLinkListAdp.clear();
            mViewModel.linkList(linksRequest);
        });
        mVDBinding.srlLinks.setEnableLoadMore(false);

        mVDBinding.titleView.setRightOnClickListener(v -> {
            ARouter.getInstance().build(ArouterPath.EDIT_LINK_ACTIVITY).navigation();
        });
    }

    @Override
    protected void initData() {
        linksRequest.setPageIndex(1);
        linksRequest.setPageSize(40);
        linksRequest.setQueryList(new ArrayList<>());
        linksRequest.setSortFields("o.createTime_d");

        mViewModel.linkList(linksRequest);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldLinkListResponseData.observe(this, data -> {
            if (mVDBinding.srlLinks.isRefreshing()) {
                mVDBinding.srlLinks.finishRefresh();
            } else if (mVDBinding.srlLinks.isLoading()) {
                mVDBinding.srlLinks.finishLoadMore();
            }
            if (data != null) {
                if (linksRequest.getPageIndex() == 1) {
                    mLinkListAdp.setNewData(data.getRecords());
                } else {
                    mLinkListAdp.addAll(data.getRecords());
                }
                if (linksRequest.getPageIndex() < data.getPages()) {
                    mVDBinding.srlLinks.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlLinks.setEnableLoadMore(false);
                }
            } else {
                mLinkListAdp.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void visibleAgain() {
        linksRequest.setPageIndex(1);
        mLinkListAdp.clear();
        mViewModel.linkList(linksRequest);
    }
}
